package jframe.interceptor;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jframe.common.JsonHelper;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * @描述:<p>Session拦截器 </p>
 *
 * @作者: 叶平平(yepp)
 *
 * @时间: 2011-11-16 下午08:38:30
 */
@SuppressWarnings("serial")
public class SessionInterceptor  extends AbstractInterceptor {

	
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
	
		//ActionContext ctx = invocation.getInvocationContext();
		  HttpServletResponse response = ServletActionContext.getResponse();
	      HttpServletRequest request = ServletActionContext.getRequest();
	      String ajaxTag = request.getHeader("Request-By");
	     	
	      if(ajaxTag == null || !ajaxTag.trim().equalsIgnoreCase("isAjax")){  
	    	  response.sendRedirect("login.jsp");  
          }else{  
        	  response.addHeader("sessionstatus", "timeout");   
              Map<String, Object> result = new HashMap<String, Object>();  
              result.put("success", false);  
              result.put("timeout",true);  
              result.put("redirectUri", "login.jsp");  
              PrintWriter out = response.getWriter();  
              out.print(JsonHelper.getJsonObject(result));  
              out.flush();  
              out.close();  
          }  
		return invocation.invoke();
	}
}
